package com.elvin.httptask;

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class AtySubmitDataByGet extends Activity {

	private EditText etUserName;
	private EditText etPassword;
	private Button btnSubmit;
	private TextView tvResult;
	
	private Handler handler = new Handler(){
		
		public void handleMessage(Message msg) {
			String result = (String) msg.obj;
			tvResult.setText(result);
		};
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.aty_submitdata_get);
		
		etUserName = (EditText) this.findViewById(R.id.etUserName1);
		etPassword = (EditText) this.findViewById(R.id.etPassword1);
		btnSubmit = (Button) this.findViewById(R.id.btnSubmit1);
		tvResult = (TextView) this.findViewById(R.id.tvResult1);
		
		btnSubmit.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				new Thread(new MyThread()).start();
			}
		});
		
		
	}
	
	class MyThread implements Runnable{

		@Override
		public void run() {
			// TODO Auto-generated method stub
			HttpClient httpClient = new DefaultHttpClient();
			
			String sendData = "?";
			sendData += "username="+etUserName.getText().toString().trim();
			sendData += "&password="+etPassword.getText().toString().trim();
			
			
            HttpGet httpGet = new HttpGet(CommonURL.resGETSUBMIT_URL+sendData);
            
            
            try {

				HttpResponse httpResponse = httpClient.execute(httpGet);
				
				if(httpResponse.getStatusLine().getStatusCode() == 200){
					
					String result = EntityUtils.toString(httpResponse.getEntity(), "utf-8");
					
					Message msg = Message.obtain();
					msg.obj = result;
					handler.sendMessage(msg);
				}
				
				
			} catch (ClientProtocolException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
          
            

		}
		
	}
	
	
}
